import React, { useState, useEffect } from 'react';
import "./index.less"
import axios from 'axios';
import { Form, Input, Button, message } from 'antd';
import { UserOutlined, LockOutlined } from '@ant-design/icons';
import { history } from 'umi';

import {encrypt} from './password';
import { loginApi } from '@/api/login'
export default function(props){
  const [state, setState] = useState()

  useEffect(() => {

  }, [])


  const onFinish =async (data) => {
    const res = await loginApi(data).then(res => {
        if(res.data.code == 200) {

            localStorage.setItem("login", JSON.stringify(res.data.data))

            history.push({  //   跳转到 首页
                pathname: '/home',
                // query: {
                //     ...res.data.data
                // }
            })
            message.success(res.data.message);

        } else if(res.data.code == 500) {
            message.error(res.data.message);
        } else if(res.data.code == 601) {
            message.error(res.data.message);
        }
    }).catch(err => {
        console.log("err:", err)
    })
    // console.log(res)
  };

  const onFinishFailed = (errorInfo) => { 
    // console.log('Failed:', errorInfo);
  };

  return (
    <div className='login_form_in_content'>
        <div className='form_title'> 秦学后台管理 </div>

        <Form
            name="basic"
            initialValues={{ userAccount: 18979307261, userPwd: encrypt("zhhdzs129")}}
            onFinish={onFinish}
            onFinishFailed={onFinishFailed}
            autoComplete="off"
            >
            <Form.Item
                name="userAccount"
                rules={[{ required: true, message: '请输入您的账号!' }]}
            >
                <Input 
                    prefix={<UserOutlined className="site-form-item-icon" />} 
                    placeholder='请输入账号' />
            </Form.Item>

            <Form.Item
                name="userPwd"
                rules={[{ required: true, message: '请输入您的密码!' }]}
            >
                <Input 
                    type="password"
                    prefix={<LockOutlined className="site-form-item-icon" />} 
                    placeholder='请输入登录密码'  />
            </Form.Item>

            <Button type="primary" htmlType="submit" block className='login_btn'>
                登录
            </Button>
            <div className='login_btn_hint'> 使用手机小程序提示的账号密码登录 </div>
        </Form>

    </div>
  )
}